<?php
if (!defined('IN_CONTEXT')) die('access violation error!');

class controller_job extends lib_controller
{
	private $operations = array();//授权操作
	private $current_user = '';//当前用户名
	private $u_id;//当前用户id
	private $ip = '';//当前用户ip
	private $current_time = '';//当前操作时间
	private $role = '';//当前角色
	private $prefix = '';//表前缀
	
	public function init($action)
	{
		$_session_ = $this->_session_;
		$this->current_user = $_session_->getSession('u_name');
		$this->role = $_session_->getSession('u_role');
		$this->u_id = $_session_->getSession('u_id');
		
		$_xml_ = $this->_xml_;
		$this->prefix = $_xml_->getConfig('data/prefix');
		
		$this->operations = lib_toolkit::getOperations();
		$this->ip = $_SERVER['REMOTE_ADDR'];
		$this->current_time = time();
		
		
		if($action == 'assignTemplatePage' || $action == 'doAssignTemplate')
		{
			if(!in_array('doAssignTemplate',$this->operations)) die("No Permission.");
		}
		
		lib_toolkit::filterSpace();
		lib_toolkit::sqlInject();
		lib_toolkit::filterJs();
	}
	
	public function myJobPage()
	{
		$_mysql_ = $this->_mysql_;
		$u_id = $this->u_id;
		$operations = $this->operations;
		$templates = array();
		$roles = array();
		$roles = explode(',',$this->role);
		$showCancelButton = false;
		
		if(in_array('doAssignTemplate',$operations))//模板分派权限
		{
			//显示模板分派页面链接
			$this->assign('assignTemplate_address', 'index.php?_m=job&_a=assignTemplatePage');
		}
		
		if(in_array('doMasterReview', $operations))//总监初审
		{
			if(!(in_array('admin', $roles) || in_array('master', $roles)))
			{
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 2 AND tpl_master_id = $u_id");
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 4 AND tpl_master_id = $u_id");//初审不通过也能看到
			}
			else
			{
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 2");
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 4");//初审不通过也能看到
			}
		}
		
		if(in_array('doAssignTester', $operations))//测试人员分配
		{
			$templates[] = $_mysql_->findAll('templates',"tpl_status = 3");
			$templates[] = $_mysql_->findAll('templates',"tpl_status = 7");//显示测试人员不通过的模板
			$templates[] = $_mysql_->findAll('templates',"tpl_status = 5");//显示正在测试模板
		}
		
		if(in_array('doSubmitTemplate', $operations))//美工正在设计模板列表
		{
			if(in_array('admin', $roles) || in_array('master', $roles))
			{
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 1");//显示未设计的模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 4");//显示总监审核不通过的模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 7");//显示测试人员不通过的模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 5");//显示正在测试模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 2");//显示总监正在审核的模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 3");//显示操作人员分配
			}
			else//其他用户只能看到自己名下的模板
			{
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 1 AND tpl_designer_id = $u_id");//显示未设计的模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 4 AND tpl_designer_id = $u_id");//显示总监审核不通过的模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 7 AND tpl_designer_id = $u_id");//显示测试人员不通过的模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 5 AND tpl_designer_id = $u_id");//显示正在测试模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 2 AND tpl_designer_id = $u_id");//显示总监正在审核的模板
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 3 AND tpl_designer_id = $u_id");//显示操作人员分配
			}
		}
		
		if(in_array('doTesterReview', $operations))//测试人员正在测试
		{
			if(in_array('admin', $roles) || in_array('master', $roles))//看到所有此状态的模板
			{
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 5");
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 7");//也能查看不通过情况
			}
			else
			{
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 5 AND tpl_tester_id = $u_id");
				$templates[] = $_mysql_->findAll('templates',"tpl_status = 7 AND tpl_tester_id = $u_id");
			}
		}
		
		if(in_array('doPublishTemplate', $operations))
		{
			$templates[] = $_mysql_->findAll('templates',"tpl_status = 6");
		}
		
		if(empty($templates))
		{
			$this->assign('isEmpty', '1');
		}
		else
		{
			$template_categories = array();
			$tmp = array();
			
			$tmp = $_mysql_->findAll("template_categories");
			
			foreach($tmp as $v)
			{
				$template_categories[$v['tpl_category_id']] = $v['tpl_category_name'];
			}
			
			$tmp = array();
			$status = array();
			
			$tmp = $_mysql_->findAll("status");
			foreach($tmp as $v)
			{
				$status[$v['s_id']] = $v['s_alias'];
			}
			
			if(in_array('doCancelTemplate', $operations))
			{
				$showCancelButton = true;
			}

			$tmp = array();
			$tmp = $_mysql_->findAll("users");
			
			$this->assign('showCancelButton', $showCancelButton);
			$this->assign('status', $status);
			$this->assign('template_categories', $template_categories);
			$this->assign('templates', $templates);
			$this->assign('users',$tmp);
		}
	}
	
	public function assignTemplatePage()
	{
		$operations = $this->operations;
		$templates = array();
		
		if(!in_array('doAssignTemplate', $operations))
		{
			die("No Permission.");
		}
		
		$_mysql_ = $this->_mysql_;
		$u_id = $this->u_id;
		$u_name = $this->current_user;
		$template_categories = array();
		$template_categories = $_mysql_->findAll("template_categories");
		
		//获取有提交设计操作权限的人员，供模板派发时人员选择
		$designers = array();
		$tmp_arr = array();
		$roles = array();
		$roles = $_mysql_->findAll('rights');
		$users = $_mysql_->findAll('users');
		$role_list = array();
		foreach($roles as $v)
		{
			$tmp_arr = explode(',',$v['r_operation']);
			if(in_array('doSubmitTemplate', $tmp_arr))
			{
				$role_list[] = $v['r_role'];
			}
		}
		
		$tmp_arr = array();
		foreach($users as $v)
		{
			$tmp_arr = explode(',',$v['u_role']);
			foreach($tmp_arr as $v1)
			{
				if(in_array($v1, $role_list))
				{
					$designers[] = $v;
					break;
				}
			}
		}
		
		$sitestar_versions = $_mysql_->findAll('sitestar_version');
		
		$curr_role = $this->role;
		$curr_roles = explode(',', $curr_role);
		
		if(!(in_array('master',$curr_roles) || in_array('admin', $curr_roles)))//只有admin和master可以指派给其他人
		{
			$this->assign('u_id1', $u_id);
			$this->assign('u_name1', $u_name);
			$this->assign('allow_assignTplFlag', '1');
		}
		
		if(empty($template_categories))
		{
			die("Template category is empty.");
		}
		
		$templates = $_mysql_->findAll("templates");
		if(!empty($templates))
		{
			$this->assign('templates', $templates);
		}
		
		$this->assign('sitestar_versions', $sitestar_versions);
		$this->assign('designers', $designers);
		$this->assign('template_categories', $template_categories);
		$this->assign('next_controller', 'job');
		$this->assign('next_action', 'doAssignTemplate');
	}
	
	public function doAssignTemplate()
	{
		$this->_layout = 'response';
		$_mysql_ = $this->_mysql_;
		
		if(empty($_POST['tpl_chinese_name']) || empty($_POST['tpl_name']) || empty($_POST['tpl_category_id']) || empty($_POST['tpl_software_version']) || empty($_POST['tpl_type']) || empty($_POST['tpl_designer_id']))
		{
			$res_arr = array('result' => 'error','errmsg' =>"Post data is empty.");
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$templates = $_mysql_->findAll('templates',"tpl_name = '{$_POST['tpl_name']}'");
		if(!empty($templates))//模板名不能重复
		{
			$res_arr = array('result' => 'error','errmsg' =>"{$_POST['tpl_name']} has existed.");
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$prefix = $this->prefix;
		$tpl_assign_time = time();
		$current_user = $this->current_user;
		$u_id = $this->u_id;
		$ip = $this->ip;
		$sql = <<<SQL
INSERT INTO {$prefix}templates (tpl_name,tpl_category_id,tpl_status,tpl_assign_time,tpl_type,tpl_designer_id,tpl_master_id,tpl_software_version,tpl_master_assign_comment,tpl_chinese_name)
VALUES ('{$_POST['tpl_name']}',{$_POST['tpl_category_id']},1,$tpl_assign_time,'{$_POST['tpl_type']}',{$_POST['tpl_designer_id']},$u_id,'{$_POST['tpl_software_version']}','{$_POST['tpl_master_assign_comment']}','{$_POST['tpl_chinese_name']}')
SQL;
		$_mysql_->customSql($sql);
		$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('add role',$tpl_assign_time,'$ip','Assign template:{$_POST['tpl_name']}','$current_user')");
		
		$res_arr = array('result' => 'ok','sucmsg' =>"Add template successfully.");
		$res_str = json_encode($res_arr);
		$this->assign('res_str', $res_str);
		return 'result';
	}
	
	public function editTemplatePage()
	{
		$tpl_id = $_GET['tpl_id'];
		$_mysql_ = $this->_mysql_;
		$u_id = $this->u_id;
		$prefix = $this->prefix;
		
		//判空操作
		if(empty($tpl_id))
		{
			die("Data is empty.");
		}
		
		$templates = $_mysql_->find("templates","tpl_id = $tpl_id");
		if(empty($templates))
		{
			die("Data is empty.");
		}
		
		//根据不同状态审查是否有操作权限
		$status = $templates['tpl_status'];
		$tmp_role = $this->role;
		$current_roles = explode(',',$tmp_role);
		$operations = $this->operations;
		
		if($status == 1)
		{
			if(!in_array('doSubmitTemplate', $operations))
			{
				die("No Permission.");
			}
			
			//非admin和master角色只可以查看自己模板
			if(!(in_array('master', $current_roles) || in_array('admin',$current_roles)))
			{
				if($templates['tpl_designer_id'] != $u_id)
				{
					die("No Permission.");
				}
			}
			$this->assign('submit_button_name','提交设计');
			$this->assign('next_action', 'doSubmitTemplate');
		}
		elseif($status == 2)
		{
			if(!(in_array('doSubmitTemplate', $operations) || in_array('doMasterReview', $operations)))
			{
				die("No Permission.");
			}
			
			//非admin和master角色只可以查看自己模板
			if(!(in_array('master', $current_roles) || in_array('admin',$current_roles)))
			{
				if($templates['tpl_designer_id'] != $u_id && $templates['tpl_master_id'] != $u_id)//只有相关设计和初审人员才能看到
				{
					die("No Permission.");
				}
				
				if($templates['tpl_master_id'] == $u_id && in_array('doMasterReview', $operations))//初审人员可以点击按钮提交
				{
					$this->assign('submit_button_name','审核通过');
					$this->assign('button_nopass', "<input type='submit' value='审核不通过'/>");
					$this->assign('next_action', 'doMasterReview');
				}
			}
			else //admin和master
			{
				if(in_array('doMasterReview', $operations))
				{
					$this->assign('submit_button_name','审核通过');
					$this->assign('button_nopass', "<input type='submit' onclick='masterUnpass();' value='审核不通过'/>");
					$this->assign('next_action', 'doMasterReview');
				}
			}
		}
		elseif($status == 3)
		{
			if(!(in_array('doSubmitTemplate', $operations) || in_array('doAssignTester', $operations)))
			{
				die("No Permission.");
			}
			
			if(!(in_array('master', $current_roles) || in_array('admin',$current_roles)))
			{
				if($templates['tpl_designer_id'] != $u_id && !in_array('doAssignTester', $operations))
				{
					die("No Permission.");
				}
			}
			
			if(in_array('doAssignTester', $operations))
			{
				$this->assign('submit_button_name','分配测试人员');
				$this->assign('next_action', 'doAssignTester');
			}
		}
		elseif($status == 4)
		{
			if(!(in_array('doSubmitTemplate', $operations) || in_array('doMasterReview', $operations)))
			{
				die("No Permission.");
			}
			
			if(!(in_array('master', $current_roles) || in_array('admin',$current_roles)))
			{
				if($templates['tpl_designer_id'] != $u_id && $templates['tpl_master_id'] != $u_id)
				{
					die("No Permission.");
				}
				
				if($templates['tpl_designer_id'] == $u_id)
				{
					if(in_array('doSubmitTemplate', $operations))
					{
						$this->assign('submit_button_name','提交设计');
						$this->assign('next_action', 'doSubmitTemplate');
					}
				}
			}
			else
			{
				if(in_array('doSubmitTemplate', $operations))
				{
					$this->assign('submit_button_name','提交设计');
					$this->assign('next_action', 'doSubmitTemplate');
				}
			}
		}
		elseif($status == 5)
		{
			if(!(in_array('doSubmitTemplate', $operations) || in_array('doAssignTester', $operations) || in_array('doTesterReview', $operations)))
			{
				die("No Permission.");
			}
			
			if(!(in_array('master', $current_roles) || in_array('admin',$current_roles)))
			{
				if($templates['tpl_designer_id'] != $u_id && $templates['tpl_tester_id'] != $u_id && $templates['tpl_tester_leader_id'] != $u_id)
				{
					die("No Permission.");
				}
				
				if($templates['tpl_tester_id'] == $u_id)
				{
					if(in_array('doTesterReview', $operations))
					{
						$this->assign('item_link',"index.php?_m=job&_a=checkItemPage&tpl_id={$templates['tpl_id']}");
						$this->assign('submit_button_name','测试通过');
						$this->assign('button_nopass', "<input type='submit' onclick='testerUnpass();' value='测试不通过'/>");
						$this->assign('next_action', 'doTesterReview');
					}
				}
			}
			else
			{
				if(in_array('doTesterReview', $operations))
				{
					$this->assign('item_link',"index.php?_m=job&_a=checkItemPage&tpl_id={$templates['tpl_id']}");
					$this->assign('submit_button_name','测试通过');
					$this->assign('button_nopass', "<input type='submit' value='测试不通过'/>");
					$this->assign('next_action', 'doTesterReview');
				}
			}
			
			$check_items1 = array();
			$template_check_items = array();
			$check_items1 = $_mysql_->customSql("SELECT count(*) AS check_items_count FROM {$prefix}check_items");
			$check_items1_count = $check_items1[0]['check_items_count'];
			
			$template_check_items = explode(',',$templates['tpl_check_items']);
			$template_items_count = count($template_check_items);
			$allow_submit_pass = false;
			if($check_items1_count == $template_items_count)
			{
				$allow_submit_pass = true;
			}
			$this->assign('allow_submit_pass', $allow_submit_pass);
		}
		elseif($status == 6)
		{
			if(!in_array('doPublishTemplate', $operations))
			{
				die("Permission denied.");
			}
			$this->assign('update_button', '<input type="submit" onclick="publisher_update();" value="修改"/>');
			$this->assign('submit_button_name','发布');
			$this->assign('next_action', 'doPublishTemplate');
		}
		elseif($status == 7)
		{
			if(!(in_array('doSubmitTemplate', $operations) || in_array('doTesterReview', $operations)))
			{
				die("Permission denied.");
			}
			
			if(!(in_array('master', $current_roles) || in_array('admin',$current_roles)))
			{
				if($templates['tpl_designer_id'] != $u_id && $templates['tpl_tester_id'] != $u_id && $templates['tpl_tester_leader_id'] != $u_id)
				{
					die("No Permission.");
				}
				
				if($templates['tpl_designer_id'] == $u_id)
				{
					if(in_array('doSubmitTemplate', $operations))
					{
						$this->assign('submit_button_name','提交设计');
						$this->assign('next_action', 'doSubmitTemplate');
					}
				}
			}
			else
			{
				if(in_array('doSubmitTemplate', $operations))
				{
					$this->assign('submit_button_name','提交设计');
					$this->assign('next_action', 'doSubmitTemplate');
				}
			}
		}
		
		//在页面中显示的变量
		$testers = array();
		$tmp_arr1 = array();
		$roles1 = array();
		$roles1 = $_mysql_->findAll('rights');
		$users1 = $_mysql_->findAll('users');
		$role_list = array();
		foreach($roles1 as $v)
		{
			$tmp_arr1 = explode(',',$v['r_operation']);
			if(in_array('doTesterReview', $tmp_arr1))
			{
				$role_list[] = $v['r_role'];
			}
		}

		$tmp_arr1 = array();
		foreach($users1 as $v)
		{
			$tmp_arr1 = explode(',',$v['u_role']);
			foreach($tmp_arr1 as $v1)
			{
				if(in_array($v1, $role_list))
				{
					$testers[] = $v;
					break;
				}
			}
		}
		
		$template_categories = $_mysql_->find("template_categories","tpl_category_id = {$templates['tpl_category_id']}");
		
		$template_status = $_mysql_->find("status","s_id = {$templates['tpl_status']}");
		$template_status_alias = $template_status['s_alias']; 
		
		$tpl_type_list = array();
		$tpl_type_list = lib_dictionary::getTplTypeList();
		$tpl_type = $tpl_type_list[$templates['tpl_type']];
		
		$user_list = array();
		$users = array();
		$user_list = $_mysql_->findAll("users");
		foreach($user_list as $v)//id => u_name
		{
			$users[$v['u_id']] = $v['u_name'];
		}
//		$master_name = $users[$templates['tpl_master_id']]; 其他用户名也可以这样获取
		
		$this->assign('testers',$testers);
		$this->assign('next_controller', 'job');
		$this->assign('templates', $templates);
		$this->assign('status',$status);
		$this->assign('template_categories', $template_categories);
		$this->assign('status_alias', $template_status_alias);
		$this->assign('tpl_type', $tpl_type);
		$this->assign('users', $users);
	}
	
	public function doSubmitTemplate()
	{
		$this->_layout = 'response';
		$operations = $this->operations;
		
		$_mysql_ = $this->_mysql_;
		$tpl_id = trim($_POST['tpl_id']);
		$u_id = $this->u_id;
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		$tpl_designer_comment = trim($_POST['tpl_designer_comment']);
		$prefix = $this->prefix;
		$current_time = $this->current_time;
		$u_name = $this->current_user;
		$ip = $this->ip;
		
		if(!in_array('doSubmitTemplate',$operations))//要有此操作授权
		{
			$res_arr = array('result' => 'error','errmsg' => 'Permission denied.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(empty($tpl_id))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$templates = $_mysql_->find('templates',"tpl_id = $tpl_id");
		if(empty($templates))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$domains = array();
		$domains = explode('.', trim($_POST['tpl_domain']));
		if($domains[0] != $templates['tpl_name'])
		{
			$res_arr = array('result' => 'error','errmsg' => "三级域名的{$domains[0]}与模板编号不符");
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$status = $templates['tpl_status'];
		
		if($status == 1 || $status == 4 || $status == 7)
		{
			if(!(in_array('admin', $roles) || in_array('master', $roles)))
			{
				if($templates['tpl_designer_id'] != $u_id)//非admin和master不能提交其他设计人员的模板
				{
					$res_arr = array('result' => 'error','errmsg' => 'No Permission.');
					$res_str = json_encode($res_arr);
					$this->assign('res_str', $res_str);
					return 'result';
				}
			}
			
			if($status == 1)
			{
				$tpl_domain = trim($_POST['tpl_domain']);
				
				if(empty($tpl_domain))
				{
					$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
					$res_str = json_encode($res_arr);
					$this->assign('res_str', $res_str);
					return 'result';
				}
				
				$sql = <<<SQL
UPDATE {$prefix}templates SET tpl_domain = '$tpl_domain',tpl_designer_comment = '$tpl_designer_comment',tpl_status = 2 WHERE tpl_id = $tpl_id
SQL;
				$_mysql_->customSql($sql);
				$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('submit template',$current_time,'$ip','submit template name:{$templates['tpl_name']}','$u_name')");
				
				$res_arr = array('result' => 'ok','sucmsg' =>"Submit templates successfully.");
				$res_str = json_encode($res_arr);
				$this->assign('res_str', $res_str);
				return 'result';
			}
			elseif($status == 4 || $status == 7)
			{
				if($status == 4) 
				{
					$tmp_status = 2;
					$desc = 'When master can not pass,';
				}
				if($status == 7) 
				{
					$tmp_status = 5;
					$desc = 'When tester can not pass,';
				} 
				$sql = <<<SQL
UPDATE {$prefix}templates SET tpl_designer_comment = '$tpl_designer_comment',tpl_status = $tmp_status WHERE tpl_id = $tpl_id
SQL;
				$_mysql_->customSql($sql);
				$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('submit template',$current_time,'$ip','{$desc}submit template name:{$templates['tpl_name']}','$u_name')");
				
				$res_arr = array('result' => 'ok','sucmsg' =>"Submit templates successfully.");
				$res_str = json_encode($res_arr);
				$this->assign('res_str', $res_str);
				return 'result';
			}
		}
		else
		{
			$res_arr = array('result' => 'error','errmsg' => 'Process error.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
	}
	
	public function doMasterReview()
	{
		$this->_layout = 'response';
		$operations = $this->operations;
		
		$_mysql_ = $this->_mysql_;
		$tpl_id = trim($_POST['tpl_id']);
		$u_id = $this->u_id;
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		$tpl_master_comment = trim($_POST['tpl_master_comment']);
		$prefix = $this->prefix;
		$current_time = $this->current_time;
		$u_name = $this->current_user;
		$ip = $this->ip;
		
		if(empty($tpl_id))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(!in_array('doMasterReview',$operations))//要有此操作授权
		{
			$res_arr = array('result' => 'error','errmsg' => 'No Permission.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$templates = $_mysql_->find('templates',"tpl_id = $tpl_id");
		if(empty($templates))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(!(in_array('admin', $roles) || in_array('master', $roles)))
		{
			if($u_id != $templates['tpl_master_id'])
			{
				$res_arr = array('result' => 'error','errmsg' => 'No Permission.');
				$res_str = json_encode($res_arr);
				$this->assign('res_str', $res_str);
				return 'result';
			}
		}
		
		if($templates['tpl_status'] != 2)
		{
			$res_arr = array('result' => 'error','errmsg' => 'Process error.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$_info = trim($_POST['_info']);
		if($_info == 'masterUnpass')
		{
			$sql = <<<SQL
UPDATE {$prefix}templates SET tpl_status = 1,tpl_master_comment = '$tpl_master_comment' WHERE tpl_id = $tpl_id
SQL;
			$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('master unpass',$current_time,'$ip','unpass template name:{$templates['tpl_name']}','$u_name')");
		}
		elseif($_info == 'masterPass')
		{
			$time = time();
			
			$sql = <<<SQL
UPDATE {$prefix}templates SET tpl_status = 3,tpl_master_comment = '$tpl_master_comment',tpl_master_allow_time = $time WHERE tpl_id = $tpl_id
SQL;
			$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('master pass',$current_time,'$ip','pass template name:{$templates['tpl_name']}','$u_name')");
		}
		
		$_mysql_->customSql($sql);
			
		$res_arr = array('result' => 'ok','sucmsg' =>"Check templates successfully.");
		$res_str = json_encode($res_arr);
		$this->assign('res_str', $res_str);
		return 'result';
	}
	
	public function doAssignTester()
	{
		$this->_layout = 'response';
		
		$operations = $this->operations;
		$_mysql_ = $this->_mysql_;
		$tpl_id = trim($_POST['tpl_id']);
		$u_id = $this->u_id;
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		$prefix = $this->prefix;
		$current_time = $this->current_time;
		$u_name = $this->current_user;
		$ip = $this->ip;
		
		if(empty($tpl_id))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(!in_array('doAssignTester',$operations))//要有此操作授权
		{
			$res_arr = array('result' => 'error','errmsg' => 'No Permission.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$templates = $_mysql_->find("templates","tpl_id = $tpl_id");
		
		if(empty($templates))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if($templates['tpl_status'] != 3)
		{
			$res_arr = array('result' => 'error','errmsg' => 'Process error.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$tpl_tester_id = trim($_POST['tpl_tester_id']);
		$_mysql_->customSql("UPDATE {$prefix}templates SET tpl_status = 5,tpl_tester_leader_id = $u_id,tpl_tester_id = $tpl_tester_id WHERE tpl_status = {$templates['tpl_status']}");
		$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('Assign testers',$current_time,'$ip','Assign testers id:{$tpl_tester_id}','$u_name')");
		
		$res_arr = array('result' => 'ok','sucmsg' =>"Assign testers successfully.");
		$res_str = json_encode($res_arr);
		$this->assign('res_str', $res_str);
		return 'result';
	}
	
	public function doTesterReview()
	{
		$this->_layout = 'response';
		
		$operations = $this->operations;
		$_mysql_ = $this->_mysql_;
		$tpl_id = trim($_POST['tpl_id']);
		$u_id = $this->u_id;
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		$prefix = $this->prefix;
		$current_time = $this->current_time;
		$u_name = $this->current_user;
		$ip = $this->ip;
		$tpl_tester_comment = trim($_POST['tpl_tester_comment']); 
		
		if(empty($tpl_id))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(!in_array('doTesterReview',$operations))//要有此操作授权
		{
			$res_arr = array('result' => 'error','errmsg' => 'No Permission.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$templates = $_mysql_->find("templates","tpl_id = $tpl_id");
		
		if(empty($templates))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(!(in_array('admin', $roles) || in_array('master', $roles)))
		{
			if($u_id != $templates['tpl_tester_id'])
			{
				$res_arr = array('result' => 'error','errmsg' => 'Permission denied.');
				$res_str = json_encode($res_arr);
				$this->assign('res_str', $res_str);
				return 'result';
			}
		}
		
		if($templates['tpl_status'] != 5)
		{
			$res_arr = array('result' => 'error','errmsg' => 'Process error.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$check_items1 = array();
		$template_items1 = array();
		
		$check_items1 = $_mysql_->customSql("SELECT COUNT(*) AS check_items_count FROM {$prefix}check_items");
		$check_items1_count = $check_items1[0]['check_items_count'];
		$template_items1 = explode(',',$templates['tpl_check_items']);
		$template_items1_count = count($template_items1);
		
		if($check_items1_count != $template_items1_count)
		{
			$res_arr = array('result' => 'error','errmsg' => 'Check items can not pass.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$_info = trim($_POST['_info']);
		
		$tpl_allow_time = time();
		if($_info == 'testerPass')//测试通过
		{
			$_mysql_->customSql("UPDATE {$prefix}templates SET tpl_status = 6,tpl_tester_comment = '$tpl_tester_comment',tpl_allow_time = $tpl_allow_time WHERE tpl_id = $tpl_id");
			$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('tester pass',$current_time,'$ip','pass template name:{$templates['tpl_name']}','$u_name')");
			$res_arr = array('result' => 'ok','sucmsg' =>"Tester successfully.",'status' => '6','forward' => 'index.php?_m=job&_a=finishedJob');
		}
		elseif($_info == 'testerUnpass' || empty($_info))//测试不通过
		{
			$_mysql_->customSql("UPDATE {$prefix}templates SET tpl_status = 7,tpl_tester_comment = '$tpl_tester_comment' WHERE tpl_id = $tpl_id");
			$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('tester unpass',$current_time,'$ip','unpass template name:{$templates['tpl_name']}','$u_name')");
			$res_arr = array('result' => 'ok','sucmsg' =>"Tester successfully.");
		}
		
		$res_str = json_encode($res_arr);
		$this->assign('res_str', $res_str);
		return 'result';
	}
	
	public function doPublishTemplate()
	{
		$this->_layout = 'response';
		
		$operations = $this->operations;
		$_mysql_ = $this->_mysql_;
		$tpl_id = trim($_POST['tpl_id']);
		$u_id = $this->u_id;
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		$prefix = $this->prefix;
		$current_time = $this->current_time;
		$u_name = $this->current_user;
		$ip = $this->ip;
		$tpl_publisher_comment = trim($_POST['tpl_publisher_comment']);
		$_info = trim($_POST['_info']);
		
		if(empty($tpl_id))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(!in_array('doPublishTemplate',$operations))//要有此操作授权
		{
			$res_arr = array('result' => 'error','errmsg' => 'No Permission.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$templates = $_mysql_->find("templates","tpl_id = $tpl_id");
		
		if(empty($templates))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if($templates['tpl_status'] != 6)
		{
			$res_arr = array('result' => 'error','errmsg' => 'Process error.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if($_info == 'publisherUpdate')
		{
			$tpl_name = trim($_POST['tpl_name']);
			$tpl_chinese_name = trim($_POST['tpl_chinese_name']);
			$tpl_domain = trim($_POST['tpl_domain']);
			$_mysql_->customSql("UPDATE {$prefix}templates SET tpl_chinese_name = '$tpl_chinese_name',tpl_name = '$tpl_name',tpl_domain = '$tpl_domain',tpl_publisher_comment = '$tpl_publisher_comment' WHERE tpl_id = $tpl_id");
			$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('update templates successfully',$current_time,'$ip','publish templates name:{$templates['tpl_name']}','$u_name')");
			$res_arr = array('result' => 'ok','sucmsg' =>"Update Template successfully.",'status' => '');
		}
		else
		{
			
			$tpl_finish_time = time();
			$tpl_name = trim($_POST['tpl_name']);
			$tpl_chinese_name = trim($_POST['tpl_chinese_name']);
			
			//------------判断发布程序参数是否合理[start]---------------
			/*
			$tpl_chinese_name = strtoupper($tpl_chinese_name);
			$num = '';
			$tmp_str1 = '';
			for($count = 0;$count < strlen($tpl_chinese_name);$count++)
			{
				$num = ord($tpl_chinese_name[$count]);
				if(($num >= 65 && $num <= 90) || ($num >= 97 && $num <= 122) || ($num >= 48 && $num <= 57))
				{
					$tmp_str1 .= $tpl_chinese_name[$count];
				}
			}
			$tmp_str1 = strtolower($tmp_str1);
			if(strpos($tpl_name, $tmp_str1) === false)
			{
				$res_arr = array('result' => 'error','errmsg' => '模板编号和模板名称描述的模板不一致');
				$res_str = json_encode($res_arr);
				$this->assign('res_str', $res_str);
				return 'result';
			}
			
			$tpl_name = strtolower($tpl_name);
			$tmp_str2 = '';
			for($count = 0;$count < strlen($tpl_name);$count++)
			{
				$num = ord($tpl_name[$count]);
				if($num >= 97 && $num <= 122)
				{
					$tmp_str2 .= $tpl_name[$count];
				}
				else
				{
					break;
				}
			}
			$curr_time = date('ymd',time());
			$param2 = $tmp_str2.'-'.$curr_time.'-'.$tmp_str1;
			
			//从233获取screenshot流水号
			$link1 = mysql_connect("localhost","root","eyudda9lB6rVk83u");
			if(!$link1)
			{
				$res_arr = array('result' => 'error','errmsg' => '数据库链接失败');
				$res_str = json_encode($res_arr);
				$this->assign('res_str', $res_str);
				return 'result';
			}
			mysql_select_db('tpl_service');
			$result = array();
			$row = array();
			$result = mysql_query("SELECT screenshot FROM ezsite_templates ORDER BY id DESC");
			$row = mysql_fetch_array($result,MYSQL_NUM);
			$screenshots = $row[0];
			mysql_free_result($result);
			for($count = 0;$count < strlen($screenshots);$count++)
			{
				$num = ord($screenshots[$count]);
				if($num >= 48 && $num <= 57)
				{
					$screenshot .= $screenshots[$count];
				}
				else
				{
					continue;
				}
			}
			$screenshot = intval($screenshot);
			$screenshot++;
			//------------判断发布程序参数是否合理[end]-----------------
			
			//---------------调用发布程序[start]-------------
			include_once('./webservice/publish.php');
			$return_value = '';
			$obj = new PublishTemplate();
			$return_value = $obj->publishProcess('1273jui!@evef',$tpl_name,$param2,$tpl_chinese_name,$screenshot,$templates['tpl_category_id']);
			*/
			//---------------调用发布程序[end]---------------
			/*
			if($return_value != 'ok')
			{
				$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('publish templates failure',$current_time,'$ip','publish templates name:{$templates['tpl_name']},$return_value','$u_name')");
				$res_arr = array('result' => 'error','errmsg' => "Publis Template failure,$return_value");
				$res_str = json_encode($res_arr);
				$this->assign('res_str', $res_str);
				return 'result';
			}
			*/
			//else
			//{
				$_mysql_->customSql("UPDATE {$prefix}templates SET tpl_status = 8,tpl_publisher_id = $u_id,tpl_finish_time = $tpl_finish_time,tpl_publisher_comment = '$tpl_publisher_comment' WHERE tpl_id = $tpl_id");
				$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('publish templates successfully',$current_time,'$ip','publish templates name:{$templates['tpl_name']}','$u_name')");
				$res_arr = array('result' => 'ok','sucmsg' =>"Publis Template successfully.",'status' => '8','forward' => 'index.php?_m=job&_a=finishedJob');
			//}
		}
		
		$res_str = json_encode($res_arr);
		$this->assign('res_str', $res_str);
		return 'result';
	}
	
	public function finishedJob()
	{
		$_mysql_ = $this->_mysql_;
		$u_id = $this->u_id;
		$tmp_roles = $this->role;
		$current_roles = explode(',', $tmp_roles);
		$roles = array();
		$roles = explode(',', $tmp_roles);
		
		$templates = array();
		$operations = $this->operations;
		$tmp = array();
		
		if(in_array('doSubmitTemplate', $operations) || in_array('doMasterReview', $operations) || in_array('doAssignTester',$operations) || in_array('doTesterReview', $operations))
		{
			if(!(in_array('master', $current_roles) || in_array('admin',$current_roles)))
			{
				$tmp = $_mysql_->findAll("templates","tpl_status = 6 AND (tpl_designer_id = $u_id OR tpl_tester_leader_id = $u_id OR tpl_master_id = $u_id OR tpl_tester_id = $u_id)");
				if(!empty($tmp))
				{ 
					$templates[] = $tmp;
				}
			}
			else
			{
				$templates[] = $_mysql_->findAll("templates","tpl_status = 6");
			}
		}
		
		if(in_array('doPublishTemplate', $operations) || in_array('doSubmitTemplate', $operations) || in_array('doMasterReview', $operations) || in_array('doAssignTester',$operations) || in_array('doTesterReview', $operations))
		{
			if(!(in_array('master', $current_roles) || in_array('admin',$current_roles) || in_array('doPublishTemplate', $operations)))
			{
				$templates[] = $_mysql_->findAll("templates","tpl_status = 8 AND (tpl_designer_id = $u_id OR tpl_tester_leader_id = $u_id OR tpl_master_id = $u_id OR tpl_tester_id = $u_id)");
			}
			else
			{
				$templates[] = $_mysql_->findAll("templates","tpl_status = 8");
			}
		}
		
//		if(in_array('doCancelTemplate', $operations))
//		{
			if(!(in_array('master', $current_roles) || in_array('admin',$current_roles)))
			{
				$templates[] = $_mysql_->findAll("templates","tpl_status = 9 AND (tpl_designer_id = $u_id OR tpl_tester_leader_id = $u_id OR tpl_master_id = $u_id OR tpl_tester_id = $u_id)");
			}
			else
			{
				$templates[] = $_mysql_->findAll("templates","tpl_status = 9");
			}
//		}
		
		$users = array();
		$tmp_users = array();
		$template_categories = array();
		$tmp_categories = array();
		$tmp_categories = $_mysql_->findAll('template_categories');
		$tmp_users = $_mysql_->findAll('users');
			
		foreach($tmp_categories as $v)
		{
			$template_categories[$v['tpl_category_id']] = $v['tpl_category_name'];
		}
			
		foreach($tmp_users as $v)
		{
			$users[$v['u_id']] = $v['u_name'];
		}
		
		$this->assign('templates', $templates);
		$this->assign('template_categories', $template_categories);
		$this->assign('users', $users);
	}
	
	public function checkItemPage()
	{
		$_mysql_ = $this->_mysql_;
		$tpl_id = $_GET['tpl_id'];
		$operations = $this->operations;
		$tmp_templates = array();
		$template = array();
		$tmp_items = array();
		$check_items = array();
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		$u_id = $this->u_id;
		
		if(empty($tpl_id))
		{
			die("Data is empty.");
		}
		
		$template = $_mysql_->find('templates',"tpl_id = $tpl_id");
		if(empty($template))
		{
			die("Data is empty.");
		}
		
		if($template['tpl_status'] != 5)
		{
			die("Process error.");
		}
		
		if(!in_array('doTesterReview', $operations))
		{
			die('Permission denied.');
		}
		
		if(!(in_array('admin', $roles) || in_array('master', $roles)))
		{
			if($u_id != $template['tpl_tester_id'])
			{
				die('No Permission.');
			}
		}
		
		$tmp_templates = explode(',',$template['tpl_check_items']);
		
		$check_items = array();
		$tmp_items = array();
		$tmp_items = $_mysql_->findAll('check_items','1 =1 ORDER BY c_type_id');
		$check_types = $_mysql_->findAll('check_types','1 =1 ORDER BY ct_id');
		foreach($check_types as $v)
		{
			foreach($tmp_items as $v1)
			{
				if($v1['c_type_id'] == $v['ct_id'])
				{
					$check_items[$v['ct_id']][$v1['c_id']] = $v1['c_desc'];
				}
			}
		}
		
		$this->assign('tmp_templates', $tmp_templates);
		$this->assign('tpl_id', $tpl_id);
		$this->assign('template', $template);
		$this->assign('check_items', $check_items);
		$this->assign('check_types', $check_types);
		$this->assign('next_controller', 'job');
		$this->assign('next_action', 'doCheckItem');
	}
	
	public function doCheckItem()
	{
		$this->_layout = 'response';
		$operations = $this->operations;
		
		$_mysql_ = $this->_mysql_;
		$tpl_id = trim($_POST['tpl_id']);
		$u_id = $this->u_id;
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		$tpl_master_comment = trim($_POST['tpl_master_comment']);
		$prefix = $this->prefix;
		$current_time = $this->current_time;
		$u_name = $this->current_user;
		$ip = $this->ip;
		$template = array();
		$check_str = '';
		
		if(empty($tpl_id))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Permission denied.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$template = $_mysql_->find("templates","tpl_id = $tpl_id");
		
		if(empty($template))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Permission denied.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if($template['tpl_status'] != 5)
		{
			die("Process error.");
		}
		
		if(!in_array('doTesterReview', $operations))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Permission denied.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(!(in_array('admin', $roles) || in_array('master', $roles)))
		{
			if($u_id != $template['tpl_tester_id'])
			{
				$res_arr = array('result' => 'error','errmsg' => 'Permission denied.');
				$res_str = json_encode($res_arr);
				$this->assign('res_str', $res_str);
				return 'result';
			}
		}
		
		foreach($_POST as $k => $v)
		{
			if($k == '_m' || $k == '_a' || $k == 'tpl_id')
			{
				continue;
			}
			$check_str .= $v.',';
		}
		$check_str = substr($check_str,0,strlen($check_str) - 1);
		$_mysql_->customSql("UPDATE {$prefix}templates SET tpl_check_items = '$check_str' WHERE tpl_id = $tpl_id");
		$res_arr = array('result' => 'ok','sucmsg' =>"Update items successfully.");
		$res_str = json_encode($res_arr);
		$this->assign('res_str', $res_str);
		return 'result';
	}
	
	public function doViewProcess()
	{
		$this->_layout = 'blank';
		$tpl_id = $_GET['id'];
		$tpl_status = $_GET['status'];
		$_mysql_ = $this->_mysql_;
		$u_id = $this->u_id;
		
		if(empty($tpl_id) || empty($tpl_status))
		{
			die("Data is empty.");
		}
		
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		
		if(!(in_array('admin', $roles) || in_array('master', $roles)))
		{
			$template = $_mysql_->find("templates","(tpl_designer_id = $u_id OR tpl_master_id = $u_id OR tpl_tester_leader_id = $u_id OR tpl_tester_id = $u_id OR tpl_publisher_id = $u_id) AND tpl_id = $tpl_id");
			if(empty($template))
			{
				die("Permission denied.");
			}
			
			if($template['tpl_status'] != $tpl_status)
			{
				die("Process error.");
			}
		}
		
		$this->assign('status', $tpl_status);
	}
	
	public function viewJobPage()
	{
		$template = array();
		$tpl_id = trim($_GET['tpl_id']);
		$_mysql_ = $this->_mysql_;
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		$u_id = $this->u_id;
		
		if(empty($tpl_id))
		{
			die("Data is empty.");
		}
		
		$template = $_mysql_->find('templates',"tpl_id = $tpl_id");
		if(empty($template))
		{
			die("Data is empty.");
		}
		
		$tpl_status = $template['tpl_status'];
		if(($tpl_status != 6) && ($tpl_status != 8) && ($tpl_status != 9))
		{
			die("Data is empty.");
		}
		
		if(!(in_array('master', $roles) || in_array('admin', $roles)))
		{
			if(!($template['tpl_designer_id'] == $u_id || $template['tpl_master_id'] == $u_id || $template['tpl_tester_leader_id'] == $u_id || $template['tpl_tester_id'] == $u_id || $template['tpl_publisher_id'] == $u_id))
			{
				die("Permission denied.");
			}
		}
		
		$user_list = array();
		$users = array();
		$user_list = $_mysql_->findAll("users");
		foreach($user_list as $v)//id => u_name
		{
			$users[$v['u_id']] = $v['u_name'];
		}
		
		$template_categories = $_mysql_->find("template_categories","tpl_category_id = {$template['tpl_category_id']}");
		$template_status = $_mysql_->find("status","s_id = {$template['tpl_status']}");
		$template_status_alias = $template_status['s_alias'];
		$tpl_type_list = array();
		$tpl_type_list = lib_dictionary::getTplTypeList();
		$tpl_type = $tpl_type_list[$template['tpl_type']];
		
		$this->assign('tpl_type', $tpl_type);
		$this->assign('template_categories', $template_categories);
		$this->assign('status_alias', $template_status_alias);
		$this->assign('templates', $template);
		$this->assign('users', $users);
	}
	
	public function doDeleteProcess()
	{
		$this->_layout = 'response';
		$_mysql_ = $this->_mysql_;
		$tpl_id = trim($_GET['id']);
		$operations = $this->operations;
		$prefix = $this->prefix;
		$u_id = $this->u_id;
		$tmp_roles = $this->role;
		$roles = array();
		$roles = explode(',', $tmp_roles);
		$current_time = $this->current_time;
		$ip = $this->ip;
		$u_name = $this->current_user;
		
		if(empty($tpl_id))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		$template = $_mysql_->find("templates","tpl_id = $tpl_id");
		
		if(empty($template))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Data is empty.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if($template['tpl_status'] == 6 || $template['tpl_status'] == 8 || $template['tpl_status'] == 9)//已完成或已撤销的模板流程不能撤销
		{
			$res_arr = array('result' => 'error','errmsg' => 'Process has finished.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(!in_array('doCancelTemplate', $operations))
		{
			$res_arr = array('result' => 'error','errmsg' => 'Permission denied.');
			$res_str = json_encode($res_arr);
			$this->assign('res_str', $res_str);
			return 'result';
		}
		
		if(!(in_array('admin', $roles) || in_array('master', $roles)))//非admin和master角色不能撤销其他人的流程
		{
			if($u_id != $template['tpl_designer_id'] && $u_id != $template['tpl_master_id'] && $u_id != $template['tpl_tester_leader_id'] && $u_id != $template['tpl_tester_id'] && $u_id != $template['tpl_publisher_id'])
			{
				$res_arr = array('result' => 'error','errmsg' => 'Permission denied.');
				$res_str = json_encode($res_arr);
				$this->assign('res_str', $res_str);
				return 'result';
			}
		}
		
		$_mysql_->customSql("UPDATE {$prefix}templates SET tpl_status = 9 WHERE tpl_id = {$template['tpl_id']}");
		$_mysql_->customSql("INSERT INTO {$prefix}logs (log_operation,log_time,log_ip,log_desc,log_operator) VALUES ('cancel templates successfully',$current_time,'$ip','publish templates name:{$template['tpl_name']}','$u_name')");
		$res_arr = array('result' => 'ok','sucmsg' =>"Cancel process successfully.");
		$res_str = json_encode($res_arr);
		$this->assign('res_str', $res_str);
		return 'result';
	}
}
?>